package com.useradmin.common.plugin;

import com.jfinal.config.Routes;
import com.jfinal.plugin.IPlugin;
import com.useradmin.common.base.BaseController;
import com.useradmin.common.tools.ToolClassSearch;
import com.useradmin.framework.aspectj.annotation.Controller;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * 扫描Controller上的注解，绑定Controller和controllerKey
 *
 */
public class ControllerPlugin implements IPlugin {

    protected final Logger log = Logger.getLogger(getClass());

    private Routes routes;

    public ControllerPlugin(Routes routes) {
        this.routes = routes;
    }

    @Override
    public boolean start() {
        // 查询所有继承BaseController的子类
        List<Class<?>> controllerClasses = ToolClassSearch.search(BaseController.class);

        // 循环处理自动注册映射
        for (Class controller : controllerClasses) {
            // 获取注解对象
            Controller controllerBind = (Controller) controller.getAnnotation(Controller.class);
            if (controllerBind == null) {
                log.warn(controller.getName() + "继承了BaseController，但是没有注解绑定映射路径，请检查是否已经手动绑定 ！！！");
                continue;
            }

            // 获取映射路径数组
            String[] controllerKeys = controllerBind.controllerKey();
            for (String controllerKey : controllerKeys) {
                controllerKey = controllerKey.trim();
                if (controllerKey.equals("")) {
                    log.error(controller.getName() + "注解错误，映射路径为空");
                    continue;
                }

                // 注册映射
                routes.add(controllerKey, controller);
                log.debug("Controller注册： controller = " + controller + ", controllerKey = " + controllerKey);
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

}